<?php
namespace app\console\controller;

use think\Db;
use think\Url;
use think\Request;
use think\Session;
use think\Controller;
use app\client\model\ShopGoodsEvaluate as ShopGoodsEvaluateModel;

class ShopGoodsEvaluate extends Controller
{

    /*
     * 评论列表
     */
    public function index()
    {

        $inquire = Request::instance()->param();

        // 搜索条件
        $condition['id'] = ['gt', 0];
        //$condition['shop_id'] = ['eq', Session::get('shop_id')];

        if (!empty($inquire['reply_status'])) $condition['reply_status'] = ['eq', $inquire['reply_status']];
        if (!empty($inquire['status'])) $condition['shop_goods_evaluate.status'] = ['eq', $inquire['status']];
        if (!empty($inquire['title'])) $condition['shop_goods.title'] = ['like', '%' . $inquire['title'] . '%'];

        // 读取
        $data = Db::view('shop_goods_evaluate', 'id,member_id,grade,title,thumb_img,attr,status,reply_status,create_time')
            ->view('member', 'id as user_id,username', 'member.id = shop_goods_evaluate.member_id','left')
            ->view('shop_goods', 'title,thumb_img', 'shop_goods_evaluate.goods_id = shop_goods.id','left')
            ->where($condition)
            ->order(['create_time' => 'desc'])
            ->paginate(10, false, ['query' => $inquire]);

        return $this->fetch('index', [
            'mate_title' => '订单评价列表',
            'data'       => $data,
        ]);

    }

    /*
     * 评论查看
     */
    public function examine($id)
    {

        if (Request::instance()->isPost()) {

            $data = Request::instance()->param();

            // 提交数据
            $dispose = (new ShopGoodsEvaluateModel())->allowField(true)->save($data, ['id' => $id]);

            if ($dispose !== false) {
                win('操作成功', Url::build('ShopGoodsEvaluate/index'));
            }

            fail('操作失败');

        }

        $item = Db::view('shop_goods_evaluate', '*')
            ->view('member', 'id as user_id,username', 'member.id = shop_goods_evaluate.member_id','left')
            ->view('shop_goods', 'title,thumb_img', 'shop_goods_evaluate.goods_id = shop_goods.id','left')
            ->where(['shop_goods_evaluate.id' => $id])
            ->find();
        // 评论图片序列化
        $item['picArr'] =  (array)unserialize($item['picArr']);


        return $this->fetch('', [
            "mate_title"   => '评论详情',
            'item'         => $item

        ]);
    }

}
